﻿using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using YArchitech.LIB;
using YArchitech.Temp.Properties;

namespace YArchitech.Temp
{
	internal class MenuStripHelper
	{
		public static void Init(Form parentForm, MenuStrip menu, ToolStripContainer toolStripContainer)
		{
			MenuStripHelper.Parent = parentForm;
			MenuStripHelper.Parent.Icon = Resource.YArchitechIcon;
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(YArchitech.Temp.Properties.Resources.Menu);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("HYStrip");
			foreach (object obj in xmlNode.SelectSingleNode("MenuStrip").SelectNodes("Menu"))
			{
				XmlNode rootNode = (XmlNode)obj;
				ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem();
				MenuStripHelper.GenerateMenusFromXML(rootNode, toolStripMenuItem);
				menu.Items.Add(toolStripMenuItem);
			}
			XmlNodeList xmlNodeList = xmlNode.SelectSingleNode("ToolStrip").SelectNodes("Tool");
			for (int i = xmlNodeList.Count - 1; i >= 0; i--)
			{
				XmlNode rootNode2 = xmlNodeList.Item(i);
				ToolStrip toolStrip = new ToolStrip();
				MenuStripHelper.GenerateToolsFromXML(rootNode2, toolStrip);
				toolStripContainer.TopToolStripPanel.Controls.Add(toolStrip);
			}
		}

		private static void ExtractAttribute(ToolStripItem strip, XmlNode xn)
		{
			strip.Name = xn.Attributes["Name"].Value;
			strip.Text = xn.Attributes["Text"].Value;
		}

		private static Image GetImage(XmlNode xn)
		{
			XmlAttribute xmlAttribute = xn.Attributes["Image"];
			if (xmlAttribute != null)
			{
				return Resource.GetImage(xmlAttribute.Value);
			}
			return null;
		}

		private static void FindEventsByName(object sender, object receiver, bool bind, XmlNode xn)
		{
			XmlAttribute xmlAttribute = xn.Attributes["OnClick"];
			if (xmlAttribute == null)
			{
				return;
			}
			string value = xmlAttribute.Value;
			EventInfo[] events = sender.GetType().GetEvents();
			Type type = receiver.GetType();
			foreach (EventInfo eventInfo in events)
			{
				MethodInfo method = type.GetMethod(string.Format("{0}{1}{2}", "StripItemOn", eventInfo.Name, value), BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic);
				if (method != null)
				{
					Delegate handler = Delegate.CreateDelegate(eventInfo.EventHandlerType, receiver, method.Name);
					if (bind)
					{
						eventInfo.AddEventHandler(sender, handler);
					}
					else
					{
						eventInfo.RemoveEventHandler(sender, handler);
					}
				}
			}
		}

		private static Keys GetShortKeys(XmlNode xn)
		{
			if (xn.Attributes["ShortcutKeys"] == null)
			{
				return Keys.None;
			}
			string[] array = xn.Attributes["ShortcutKeys"].Value.Split(MenuStripHelper.sp, StringSplitOptions.RemoveEmptyEntries);
			string text = array[0];
			string a = text.ToUpper();
			if (!(a == "CTRL"))
			{
				if (!(a == "ALT"))
				{
					if (!(a == "SHIFT"))
					{
						if (a == "DEL")
						{
							text = "Delete";
						}
					}
					else
					{
						text = "Shift";
					}
				}
				else
				{
					text = "Alt";
				}
			}
			else
			{
				text = "Control";
			}
			Keys keys = (Keys)Enum.Parse(typeof(Keys), text);
			if (array.Length > 1)
			{
				Keys keys2 = (Keys)Enum.Parse(typeof(Keys), array[1]);
				keys |= keys2;
			}
			return keys;
		}

		private static void GenerateMenusFromXML(XmlNode rootNode, ToolStripMenuItem menuItem)
		{
			MenuStripHelper.ExtractAttribute(menuItem, rootNode);
			foreach (object obj in rootNode.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				string name = xmlNode.Name;
				if (!(name == "Separator"))
				{
					if (!(name == "Recent"))
					{
						if (name == "MenuItem")
						{
							ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem();
							MenuStripHelper.GenerateMenusFromXML(xmlNode, toolStripMenuItem);
							toolStripMenuItem.Image = MenuStripHelper.GetImage(xmlNode);
							toolStripMenuItem.ShortcutKeys = MenuStripHelper.GetShortKeys(xmlNode);
							MenuStripHelper.FindEventsByName(toolStripMenuItem, MenuStripHelper.Parent, true, xmlNode);
							menuItem.DropDownItems.Add(toolStripMenuItem);
						}
					}
				}
				else
				{
					menuItem.DropDownItems.Add(new ToolStripSeparator());
				}
			}
		}

		private static void ExtractAttribute(ToolStrip strip, XmlNode xn)
		{
			strip.Name = xn.Attributes["Name"].Value;
			strip.Text = xn.Attributes["Text"].Value;
		}

		private static void GenerateToolsFromXML(XmlNode rootNode, ToolStrip tool)
		{
			tool.Dock = DockStyle.None;
			MenuStripHelper.ExtractAttribute(tool, rootNode);
			foreach (object obj in rootNode.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				string name = xmlNode.Name;
				if (!(name == "Separator"))
				{
					if (name == "ToolItem")
					{
						ToolStripButton toolStripButton = new ToolStripButton();
						toolStripButton.DisplayStyle = ToolStripItemDisplayStyle.Image;
						toolStripButton.TextImageRelation = TextImageRelation.ImageAboveText;
						MenuStripHelper.ExtractAttribute(toolStripButton, xmlNode);
						toolStripButton.Image = MenuStripHelper.GetImage(xmlNode);
						toolStripButton.ImageTransparentColor = Color.Transparent;
						toolStripButton.ImageScaling = ToolStripItemImageScaling.None;
						MenuStripHelper.FindEventsByName(toolStripButton, MenuStripHelper.Parent, true, xmlNode);
						tool.Items.Add(toolStripButton);
					}
				}
				else
				{
					tool.Items.Add(new ToolStripSeparator());
				}
			}
		}

		private static Form Parent = null;

		private static char[] sp = new char[]
		{
			'+'
		};

		private const string attribute_name = "Name";

		private const string attribute_Text = "Text";
	}
}
